package com.estate.struts.actions;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.estate.business.TenderService;
import com.estate.utils.ProjectConstants;
import com.estate.vo.Vo;
import com.estate.vo.impl.DeleProjectVo;
import com.estate.vo.impl.TenderVo;

public class TenderAction extends EstateSupport {

	private String id;
	private String tenderName;
	private String postPackageType;
	private String explain;
	private String valuation;
	private String planPostDate;
	private String tenderType;
	private String workTime;
	private String planBeginDate;
	private String planOverDate;
	private String deleagency;
	private String delePeople;
	private String delePhone;
	private String remark;
	private String handlePeople;
	private String handlePhone;
	private String deleProjectId;
	private String tenderNum;

	private String method;

	private String multiType;

	private String multiDelete;
	private String delStr;

	private TenderService tenderService;
	
	private String directPage;

	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if ("add".equals(this.getMethod())) {
			TenderVo tender = this.getAddTender();
			if (tender != null) {
				boolean result = this.getTenderService().addTender(tender);
				if (result) {
					page = this.SUCCESS;
				} else {
					page = "fail";
				}
			} else {
				page = "fail";
			}
		} else if ("list".equals(this.getMethod())) {
			List<Vo> tenderList = this.getTenderService().getList();
			this.getRequest()
					.setAttribute("tenderList", tenderList);
			if (this.getMultiType() != null
					&& this.getMultiType().equals("true")) {
				this.getRequest().setAttribute("multiType", new Boolean(true));
			} else {
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page = "allTender";
		} else if ("detail".equals(this.getMethod())) {
			Vo vo = this.getTenderService().getTender(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tender", vo);
			page = "detail";
		} else if ("modify".equals(this.getMethod())) {
			Vo vo = this.getTenderService().getTender(
					Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tender", vo);
			page = "update";
		} else if ("update".equals(this.getMethod())) {
			TenderVo tender = this.getUpdateTender();
			if (tender == null) {
				page = "fail";
			} else {
				boolean result = this.getTenderService()
						.updateTender(tender);
				if (result) {
					page = this.SUCCESS;
				} else {
					page = "fail";
				}
			}
		}
//		else if ("post".equals(this.getMethod())) {
//			this.getTenderService().updatePost(
//					Integer.parseInt(this.getId()));
//			page = this.SUCCESS;
//		} 
		else if ("delete".equals(this.getMethod())) {
			if ("true".equals(this.multiDelete)) {
				// System.out.println(this.getDelStr());
				// System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getTenderService().deleteMultiTender(this.getDelStr());
				if (result) {
					page = this.SUCCESS;
				} else {
					page = "fail";
				}

			} else {
				// System.out.println("single delete");
				if (this.getId() == null || "".equals(this.getId())) {
					page = "faild";
				} else {
					TenderVo tender = new TenderVo();
					tender.setId(Integer.parseInt(this.getId()));
					boolean result = this.getTenderService()
							.deleteTender(tender);
					if (result) {
						page = this.SUCCESS;
					} else {
						page = "fail";
					}
				}
			}
		} else {
			page = "fail";
		}
		if(this.getDirectPage() != null && !"".equals(this.getDirectPage())){
			page = this.getDirectPage();
		}
		return page;
	}

	private TenderVo getUpdateTender() {
		TenderVo tender = new TenderVo();

		try {
			int id = Integer.parseInt(this.getId());
			
			tender.setId(id);
			tender.setDeleagency(this.getDeleagency());
			tender.setDelePeople(this.getDelePeople());
			tender.setDelePhone(this.getDelePhone());
			tender.setTenderNum(this.getTenderNum());
			tender.setExplain(this.getExplain());
			tender.setHandlePeople(this.getHandlePeople());
			tender.setHandlePhone(this.getHandlePhone());
			tender.setPlanBeginDate(ProjectConstants.SIMPLE_DATE.parse(this.getPlanBeginDate()));
			tender.setPlanOverDate(ProjectConstants.SIMPLE_DATE.parse(this.getPlanOverDate()));
			tender.setPlanPostDate(ProjectConstants.SIMPLE_DATE.parse(this.getPlanPostDate()));
			tender.setPostPackageType(this.getPostPackageType());
			
			tender.setRemark(this.getRemark());
			tender.setTenderName(this.getTenderName());
			tender.setTenderType(this.getTenderType());
			tender.setValuation(this.getValuation());
			tender.setWorkTime(this.getWorkTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tender;
	}

	private TenderVo getAddTender() {
		TenderVo tender = new TenderVo();

		try {
			tender.setDeleagency(this.getDeleagency());
			tender.setTenderNum(this.getTenderNum());
			tender.setDelePeople(this.getDelePeople());
			tender.setDelePhone(this.getDelePhone());
			tender.setExplain(this.getExplain());
			tender.setHandlePeople(this.getHandlePeople());
			tender.setHandlePhone(this.getHandlePhone());
			tender.setPlanBeginDate(ProjectConstants.SIMPLE_DATE.parse(this.getPlanBeginDate()));
			tender.setPlanOverDate(ProjectConstants.SIMPLE_DATE.parse(this.getPlanOverDate()));
			tender.setPlanPostDate(ProjectConstants.SIMPLE_DATE.parse(this.getPlanPostDate()));
			tender.setPostDate(new Date());
			tender.setPostPackageType(this.getPostPackageType());
			tender.setPostUser(this.getSessionUser());
			
			DeleProjectVo project = new DeleProjectVo();
			project.setId(Integer.parseInt(this.getDeleProjectId()));
			tender.setPro(project);
			
			tender.setRemark(this.getRemark());
			tender.setTenderName(this.getTenderName());
			tender.setTenderType(this.getTenderType());
			tender.setValuation(this.getValuation());
			tender.setWorkTime(this.getWorkTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return tender;
	}


	public String getTenderName() {
		return tenderName;
	}

	public void setTenderName(String tenderName) {
		this.tenderName = tenderName;
	}

	public String getPostPackageType() {
		return postPackageType;
	}

	public void setPostPackageType(String postPackageType) {
		this.postPackageType = postPackageType;
	}

	public String getExplain() {
		return explain;
	}

	public void setExplain(String explain) {
		this.explain = explain;
	}

	public String getValuation() {
		return valuation;
	}

	public void setValuation(String valuation) {
		this.valuation = valuation;
	}


	public String getTenderType() {
		return tenderType;
	}

	public void setTenderType(String tenderType) {
		this.tenderType = tenderType;
	}

	public String getWorkTime() {
		return workTime;
	}

	public void setWorkTime(String workTime) {
		this.workTime = workTime;
	}


	public String getDeleagency() {
		return deleagency;
	}

	public void setDeleagency(String deleagency) {
		this.deleagency = deleagency;
	}

	public String getDelePeople() {
		return delePeople;
	}

	public void setDelePeople(String delePeople) {
		this.delePeople = delePeople;
	}

	public String getDelePhone() {
		return delePhone;
	}

	public void setDelePhone(String delePhone) {
		this.delePhone = delePhone;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getHandlePeople() {
		return handlePeople;
	}

	public void setHandlePeople(String handlePeople) {
		this.handlePeople = handlePeople;
	}

	public String getHandlePhone() {
		return handlePhone;
	}

	public void setHandlePhone(String handlePhone) {
		this.handlePhone = handlePhone;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setPlanPostDate(String planPostDate) {
		this.planPostDate = planPostDate;
	}

	public void setPlanBeginDate(String planBeginDate) {
		this.planBeginDate = planBeginDate;
	}

	public void setPlanOverDate(String planOverDate) {
		this.planOverDate = planOverDate;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public TenderService getTenderService() {
		return tenderService;
	}

	public void setTenderService(TenderService tenderService) {
		this.tenderService = tenderService;
	}

	public String getId() {
		return id;
	}

	public String getPlanPostDate() {
		return planPostDate;
	}

	public String getPlanBeginDate() {
		return planBeginDate;
	}

	public String getPlanOverDate() {
		return planOverDate;
	}

	public String getDeleProjectId() {
		return deleProjectId;
	}

	public void setDeleProjectId(String deleProjectId) {
		this.deleProjectId = deleProjectId;
	}

	public String getTenderNum() {
		return tenderNum;
	}

	public void setTenderNum(String tenderNum) {
		this.tenderNum = tenderNum;
	}

	public String getDirectPage() {
		return directPage;
	}

	public void setDirectPage(String directPage) {
		this.directPage = directPage;
	}
	
}
